<template>
  <div class="UpdateName-wrap">
    <!-- 头部导航 -->
    <van-nav-bar
      title="昵称"
      left-text="返回"
      right-text="完成"
      left-arrow
      @click-left="$emit('colseName')"
      @click-right="onOver"
    />
    <!---->
    <div style="padding: 10px">
      <van-field
        v-model="localName"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
        style="background-color: white"
      />
    </div>
  </div>
</template>
<script>
import { changeUserProfile } from '../../../api/profile'
export default {
  name: 'UpdateName',
  data () {
    return {
      localName: this.value
    }
  },
  components: {},
  props: {
    // 个人资料
    value: {
      type: String,
      required: true
    }
  },
  methods: {
    // 完成
    async onOver () {
      try {
        const params = {
          name: this.localName
        }
        await changeUserProfile(params) // 发送请求修改昵称
        this.$toast.success('修改成功')
        this.$emit('handleOver') // 手动修改昵称
        this.$emit('input', this.localName)
      } catch (err) {
        if (err && err.response && err.response.status === 409) {
          this.$toast.fail('昵称已重复')
        }
      }
    }

  }
}
</script>
<style scoped lang="less">
</style>
